﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;


namespace Utils
{
    /// <summary>
    ///  DataTable 对象转 list 工具类
    /// </summary>
    public static class ListTable
    {
            /// <summary>
            /// DataTable对象转换成List<T>对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="table"></param>
            /// <returns></returns>
            public static List<T> DataTableToList<T>(DataTable table)
            {
                if (table == null)
                {
                    return null;
                }

                List<DataRow> rows = new List<DataRow>();

                foreach (DataRow row in table.Rows)
                {
                    rows.Add(row);
                }

                List<T> list = null;

                if (rows != null)
                {
                    list = new List<T>();

                    foreach (DataRow row in rows)
                    {
                        T item = DataRowToObject<T>(row);
                        list.Add(item);
                    }
                }

                return list;
            }


            private static List<T> ConvertTo<T>(List<DataRow> rows)
            {
                List<T> list = null;

                if (rows != null)
                {
                    list = new List<T>();

                    foreach (DataRow row in rows)
                    {
                        T item = DataRowToObject<T>(row);
                        list.Add(item);
                    }
                }

                return list;
            }


            /// <summary>
            /// DataRow对象转换成Object<T>对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="row"></param>
            /// <returns></returns>
            public static T DataRowToObject<T>(DataRow row)
            {
                T obj = default(T);
                if (row != null)
                {
                    obj = Activator.CreateInstance<T>();

                    foreach (DataColumn column in row.Table.Columns)
                    {
                        PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                        try
                        {
                            object value = row[column.ColumnName];
                             prop.SetValue(obj, value);
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
                return obj;
            }


            /// <summary>
            /// List<T>对象转换成DataTable对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="collection"></param>
            /// <returns></returns>
            public static DataTable ListToDataTable<T>(List<T> collection)
            {
                var props = typeof(T).GetProperties();
                var dt = new DataTable();
                dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
                if (collection.Count() > 0)
                {
                    for (int i = 0; i < collection.Count(); i++)
                    {
                        ArrayList tempList = new ArrayList();
                        foreach (PropertyInfo pi in props)
                        {
                            object obj = pi.GetValue(collection.ElementAt(i), null);
                            tempList.Add(obj);
                        }
                        object[] array = tempList.ToArray();
                        dt.LoadDataRow(array, true);
                    }
                }
                return dt;
            }




        }


}
